package com.service.Hua.Impl;

import com.mapper.Hua.MaterialWarehouseOutMapper;
import com.pojo.Hua.*;
import com.service.Hua.IMaterialWarehouseOutService;
import com.util.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MaterialWarehouseOutServiceImpl implements IMaterialWarehouseOutService {
    @Autowired
    MaterialWarehouseOutMapper materialWarehouseOutMapper;

    @Override
    public List<HuaMaterialOutWarehouse> getOutWarehouse() {
        return materialWarehouseOutMapper.getOutWarehouse();
    }

    @Override
    public HuaHao getInfo() {
        HuaHao huaHao = new HuaHao();
        OutEveryView needEvery = materialWarehouseOutMapper.getNeedEvery();
        System.out.println(needEvery);
        String materialName = needEvery.getMaterialName();
        String materialPlyName = needEvery.getMaterialPlyName();
        Long materialNum = needEvery.getMaterialNum();
        System.out.println(materialNum);
        Long productNum = needEvery.getProductNum();
        System.out.println(productNum);
        Long materialNums = materialNum * productNum;
        huaHao.setMaterialName(materialName);
        huaHao.setMaterialPlyName(materialPlyName);
        huaHao.setNums(materialNums);

        return huaHao;
    }

    @Override
    public Res addOutWarehouse(Long staffId) {
        Res res = new Res();
        OutEveryView needEvery = materialWarehouseOutMapper.getNeedEvery();
        System.out.println(needEvery+"111111111111111111");
        String productionOrderCode = needEvery.getProductionOrderCode();
        Long size = needEvery.getProductionSize();
        Long materialId = needEvery.getProductionMaterial();
        Long categoryId = needEvery.getProductionCategory();
        Long plyId = needEvery.getPlyId();
        Long materialNum = needEvery.getMaterialNum();
        Long state = needEvery.getState();
        Long productNum = needEvery.getProductNum();
        //需求总数
        Long needMaterialNums = materialNum * productNum;
        HuaMaterialWarehouse warehouseNums = materialWarehouseOutMapper.getWarehouseNums(materialId, plyId);
        //仓库剩余数
        Long materialNums = warehouseNums.getMaterialNums();
        //如果仓库剩余数大于或者等于需求数量
        if (materialNums >= needMaterialNums) {
            List<HuaMaterialWarehouse> bathList = materialWarehouseOutMapper.getBath(materialId, plyId);
            for (HuaMaterialWarehouse material : bathList) {
                //得到最早批号的数量
                Long minTimeMaterialNums = material.getMaterialNums();
                //得到最早的批号
                String minTimeMaterialCode = material.getMaterialCode();
                //得到需求的剩余数量，如果剩余的需求数大于零，则清空最早批次，进行下一个批次的减少
                Long needResidueNums = needMaterialNums - minTimeMaterialNums;
                if (needResidueNums >= 0) {
                    int i = materialWarehouseOutMapper.setWarehouseOutLogic(minTimeMaterialCode);
                    if (i > 0) {
                        res.setCode(200);
                        res.setMsg("出库成功");
                    } else {
                        res.setCode(401);
                        res.setMsg("删除原材料失败");
                    }
                }else{
                    int i = materialWarehouseOutMapper.addOutWar(minTimeMaterialCode, materialId, plyId, materialNums, staffId);
                    if (i>0){
                        res.setCode(200);
                        res.setMsg("出库成功！");
                    }else{
                        res.setCode(400);
                        res.setMsg("出库失败");
                    }

                }
            }
        } else {
            HuaNeedOrder huaNeedOrder = new HuaNeedOrder();
            huaNeedOrder.setNeedMaterialId(materialId);
            huaNeedOrder.setNeedPlyId(plyId);
            huaNeedOrder.setNeedNums(needMaterialNums);
            int i = materialWarehouseOutMapper.addNeedOrder(huaNeedOrder);
            if (i > 0) {
                res.setCode(200);
                res.setMsg("发送原材料订单成功");
            } else {
                res.setCode(500);
                res.setMsg("添加失败，请联系管理员");
            }
        }
        return res;
    }
}
